import os

from celery import Celery

from blog import create_app


def make_celery(app):

	#:
	celery_first = Celery(
		app.import_name,
		backend=app.config['CELERY_RESULT_BACKEND'],
		broker=app.config['CELERY_BROKEN_URL'])
	
	#: 
	celery_first.conf.update(app.config)
	TaskBase = celery.TaskBase
	
	#:
	class ContextTask(TaskBase):
		
		abstract = True
		
		def __call__(self, *args, **kwargs):
			with app.app_context():
				return TaskBase.__call__(self, *args, **kwargs)
	
	
	#:
	celery_first.Task = ContextTask
	return celery_first


env = os.environ.get('Blog_ENV', 'dev')
flask_app = create_app('blog.config.%sConfig' % env.capitalize())
celery_first = make_celery(flask_app)